<?php


namespace App\Model;


use App\Utility\FinanceUtil;
use App\Utility\Pool\MysqlPool;
use App\Utility\Pool\RedisPool;

class CacheTable
{
    const SHARELINK_IP_CONN_CACHE = 'cz_sharelink_ip_conn_';
    const SHARE_LINK_CACHE = 'cz_share_link_';


    const SIGNIN_IP_CONN_CACHE = 'cz_signin_ip_conn_';
    const GUSEE_IP_CONN_CACHE = 'cz_guess_ip_conn_';
    const PAY_IP_CONN_CACHE = 'cz_pay_ip_conn_';


    const SIGNIN_CACHE = 'cz_signin_';
    const IS_GUESS_CACHE = 'cz_is_guess_';
    const IS_APPOINTMENT = 'cz_is_appointment';

    const INVITE_USER_CACHE = 'cz_invite_user_';

    const BEST_WINER_USER = 'cz_best_winer_user_';

    const IS_FOLLOW = 'cz_is_follow_';

    public static function getRedPack($key)
    {
        $redis = RedisPool::defer();
        return intval($redis->get('cz_red_pack_' . $key));
    }

    public static function setRedPack($key, $value)
    {
        $redis = RedisPool::defer();
        $redis->set('cz_red_pack_' . $key, $value, FinanceUtil::now2todayend() + 36000);
    }

    public static function decRedPack($key)
    {
        $redis = RedisPool::defer();
        return $redis->decr('cz_red_pack_' . $key);
    }


    public static function getFinanceConfig()
    {
        $key = 'cz_finance_config';
        $redis = RedisPool::defer();

        $config = json_decode($redis->get($key), true);

        if (!$config) {
            $db = MysqlPool::defer();
            $config = $db->getOne(DataTable::FINANCE_CONFIG);

            $redis->set($key, json_encode($config), FinanceUtil::now2todayend());
        }


        return $config;
    }

}